#include <iostream>
#include <curl/curl.h>
 
// 2. HTTP POST请求
 
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}
 
int main()
{
    CURL *curl;
    CURLcode res;
    std::string readBuffer;
    const char *postFields = "field1=value1&field2=value2";
 
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if(curl) {
        // 指定URL
        curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/post");
        // 指定post内容
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        // 执行CURL访问网站
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        } else {
            std::cout << readBuffer << std::endl;
        }
 
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
 
    return 0;
}
